package com.shushan.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.shushan.base.Constants;
import com.shushan.lockscreen.EyeSleepFloatWindow;
import com.shushan.lockscreen.EyeSleepWindowManager;
import com.shushan.lockscreen.LockScreenWindowManager;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.ui.CountdownTextView;
import com.shushan.util.SharedPreferencesUtils;

/**
 * @ClassName: SleepLockReceiver
 * @Desciption: 视力保护锁屏广播接收器
 * @author: yichaohua
 * @date: 2018-07-24
 */
public class SleepLockReceiver extends BroadcastReceiver {

    private static final String TAG = "SleepLockReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isEyeOpen = SharedPreferencesUtils.getBooleanDate(Constants.PROJECT_EYE_SWITCH_SHAREPREFERS_KEY);
        boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
        Log.i(TAG,"onReceive() isEyeOpen=" + isEyeOpen + " isChildControl=" + isChildControl);

        if(isEyeOpen && isChildControl){
            BabyApplication babyApplication = BabyApplication.getInstance();

            EyeSleepWindowManager.createEyeFloatWindow(context);
            babyApplication.setScreenOffTime(System.currentTimeMillis());

            PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            boolean isOpen = powerManager.isScreenOn();
            Log.i(TAG,"isOpen=" + isOpen);
            if(!isOpen){
                EyeSleepFloatWindow eyeSleepFloatWindow = EyeSleepWindowManager.getEyeSleepFloatWindow();
                if(eyeSleepFloatWindow != null){
                    CountdownTextView countdownTextView = eyeSleepFloatWindow.getCountdownTextView();
                    if(countdownTextView != null){
                        countdownTextView.stop();
                    }

                }
            }
        }
    }
}
